package cn.mall.org.controller;

import cn.mall.auth.annotation.MyPermission;
import cn.mall.org.dto.ManagerRoleDTO;
import cn.mall.org.service.IManagerService;
import cn.mall.org.domain.Manager;
import cn.mall.org.query.ManagerQuery;
import cn.mall.base.utils.PageList;
import cn.mall.base.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/manager")
@MyPermission(name = "账号管理")
public class ManagerController {
    @Autowired
    public IManagerService managerService;


    /**
     * 保存和修改公用的
     *
     * @param manager 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @MyPermission(name = "账号保存和修改", desc = "账号保存和修改")
    public AjaxResult addOrUpdate(@RequestBody Manager manager) {
        try {
            if (manager.getId() != null)
                managerService.update(manager);
            else
                managerService.insert(manager);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @MyPermission(name = "删除账号",desc = "删除账号")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            managerService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids) {
        try {
            managerService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败！" + e.getMessage());
        }
    }

    /**
     * 根据Id获取用户
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult loadById(@PathVariable("id") Long id) {
        try {
            Manager manager = managerService.loadById(id);
            return AjaxResult.me().setResultObj(manager);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping
    public AjaxResult loadAll() {

        try {
            List<Manager> list = managerService.loadAll();
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    @MyPermission(name = "账号分页查询", desc = "账号分页查询")
    public AjaxResult pageList(@RequestBody ManagerQuery query) {
        try {
            PageList<Manager> pageList = managerService.pageList(query);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }


    /**
     * 角色回显
     * @param managerId
     * @return
     */



    @GetMapping("/role/{managerId}")
    public AjaxResult getRoleByRoleId(@PathVariable("managerId") Long managerId){
        try {
            List<Long> roleIds = managerService.getRoleByManager(managerId);
            return AjaxResult.me().setResultObj(roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败" + e.getMessage());
        }
    }

    /**
     * 设置角色的提交接口
     * @param dto
     * @return
     */
    @PostMapping("/role")
    @MyPermission(name = "设置角色", desc = "设置角色")

    public AjaxResult saveRole(@RequestBody ManagerRoleDTO dto){
        try {
            managerService.saveRole(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败" + e.getMessage());
        }
    }

}
